﻿using UnityEngine;
using System.Collections;

public class				entity			: MonoBehaviour, base_entity
{
	// attributes
	public float			hp;

	public virtual void		create			()					{}
	public virtual void		init			()					{}

    public virtual void		damage          (float hp)			{
		if (this.hp == Mathf.Epsilon)		return;

		this.hp = Mathf.Max(Mathf.Epsilon, this.hp - hp);
		
		if (this.hp == Mathf.Epsilon)		zombie(); 
	}
	
	public virtual void		zombie			()					{}

    public virtual void		die             ()					{ 
		e_server.instance.unregister		(this); 
	}

	public virtual void		trigger_enter	(Collider other)	{}
	public virtual void		trigger_stay	(Collider other)	{}
	public virtual void		trigger_exit	(Collider other)	{}

	public virtual void		update			(float dt_s)		{}

	void					Awake			()					{ create(); }
	void					Start			()					{ init(); }
	void					Update			()					{ update(Time.deltaTime); }
	
	void					OnTriggerEnter	(Collider other)	{ trigger_enter(other); }	
	void					OnTriggerStay	(Collider other)	{ trigger_stay(other); }
	void					OnTriggerExit	(Collider other)	{ trigger_exit(other); }

	private static Hashtable unique			= new Hashtable();
	public void				add_anim_end_event(string name)
	{
		string evkey						= GetType() + "." + name;

		if (unique.ContainsKey(evkey))		return;

		AnimationClip clip					= animation[name].clip;
		AnimationEvent ev					= new AnimationEvent();
		ev.functionName						= name + "_anim_end";
		ev.time								= clip.length;
		ev.messageOptions					= SendMessageOptions.RequireReceiver;
		clip.AddEvent						(ev);

		unique.Add							(evkey, ev);

		Debug.Log							("added anim_end event " + evkey);
	}
}
